package com.woniu108.product.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.ProductActiveDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.product.param.DeleteActiveParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.woniu108.product.form.InsertActiveForm;
import com.woniu108.product.form.UpdateActiveForm;
import com.woniu108.product.param.InsertActiveParam;
import com.woniu108.product.param.UpdateActiveParam;
import com.woniu108.product.service.ProductActiveService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品活动表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/productActive")
@Api(tags = "活动接口")
public class ProductActiveController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ProductActiveService productActiveService;
    /**
     * 查询所有符合条件活动
     * @param activeId 活动id
     * @return 活动详情集合
     */
    @GetMapping("/findAllActive.c")
    @ApiOperation("查询所有符合条件活动")
    @ApiImplicitParam(name = "activeId", value = "活动ID")
    public Result<List<ProductActiveDto>> findAllActive(Integer activeId) {
        List<ProductActiveDto> list = productActiveService.findAllActive(activeId);
        return Result.newListInstance(ProductActiveDto.class).setData(list).setMsg("查询所有符合条件活动成功");
    }
    /**
     * 查询商品总览id条件活动
     * @param pItemId 商品总览id
     * @return 活动详情集合
     */
    @GetMapping("/findAllActiveByItemId.c")
    @ApiOperation("查询所有符合条件活动")
    @ApiImplicitParam(name = "pItemId", value = "商品总览ID")
    public Result<List<ProductActiveDto>> findAllActiveByItemId(Integer pItemId) {
        List<ProductActiveDto> list = productActiveService.findAllActiveByItemId(pItemId);
        return Result.newListInstance(ProductActiveDto.class).setData(list).setMsg("查询所有符合条件活动成功");
    }
    /**
     * 新增活动
     * @param form 商品编号,开始时间,结束时间,活动折扣
     * @return boolean值
     */
    @PostMapping("/insertActive.api")
    @ApiOperation("新增活动")@AdminHistoryLog
    public Result<Boolean> insertActive(@RequestBody InsertActiveForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertActiveParam param = BeanUtil.toBean(form, InsertActiveParam.class);
        param.setAdminId(adminId);
        Boolean bool = productActiveService.insertActive(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增活动成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增活动失败").setData(false).setCode(500);
    }
    /**
     * 修改活动时间
     * @param form 活动id,开始时间,结束时间,活动折扣
     * @return boolean值
     */
    @PostMapping("/updateActive.api")
    @ApiOperation("修改活动时间")@AdminHistoryLog
    public Result<Boolean> updateActive(@RequestBody UpdateActiveForm form, @RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateActiveParam param = BeanUtil.toBean(form, UpdateActiveParam.class);
        param.setAdminId(adminId);
        Boolean bool = productActiveService.updateActive(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改活动时间成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改活动时间失败").setData(false).setCode(500);
    }
    /**
     * 删除活动
     * @param activeId 活动id
     * @return boolean值
     */
    @GetMapping("/deleteActive.api")
    @ApiOperation("删除活动")@AdminHistoryLog
    @ApiImplicitParam(name = "activeId", value = "活动ID")
    public Result<Boolean> deleteActive(Integer activeId, @RequestHeader String token){
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteActiveParam param =new DeleteActiveParam(activeId,adminId);
        param.setAdminId(adminId);
        Boolean bool = productActiveService.deleteActive(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除活动成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除活动失败").setData(false).setCode(500);
    }
}

